package com.example.dryclean_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dryclean_admin.common.QueryPageParam;
import com.example.dryclean_admin.common.Result;
import com.example.dryclean_admin.entity.Comment;
import com.example.dryclean_admin.entity.Orderlist;
import com.example.dryclean_admin.entity.User;
import com.example.dryclean_admin.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiaxin
 * @since 2023-12-29
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    public ICommentService iCommentService;

    @RequestMapping("/usercommentlist")
    public Result userlist(@RequestParam String username){
        List list = iCommentService.lambdaQuery().eq(Comment::getUsername,username).list();
        return Result.success(list);
    }

    @RequestMapping("/list")
    public Result list(){
        return Result.success(iCommentService.list());
    }

    @RequestMapping("/del")
    public Result delComment(@RequestParam String id){
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<Comment>();
        if(StringUtils.hasLength(id) && !"null".equals(id)){
            qw.eq(Comment::getId, id);
            Comment comment = iCommentService.getOne(qw);
            comment.setContent("");
            iCommentService.updateById(comment);
            return Result.success();
        }
        return Result.fail();
    }

    @RequestMapping("/update")
    public Result updateComment(@RequestBody Comment comment){
        return iCommentService.updateById(comment)?Result.success():Result.fail();
    }

    @RequestMapping("/save")
    public Result save(@RequestBody Comment comment){
        return iCommentService.save(comment)?Result.success():Result.fail();
    }

    @PostMapping("/listPageR")
    public Result listPageR(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        Page<Comment> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(username) && !"null".equals(username)){
            lambdaQueryWrapper.like(Comment::getUsername,username);
        }

        IPage result = iCommentService.page(page,lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());


        return Result.success(result.getRecords(), result.getTotal());
    }
}
